(function (window, $, undefined) {
  'use strict';

  var dictData = {
    state: {
      '0': '启用',
      '1': '停用'
    },
    autoBrandId: {},
    cityId: {}
  }

  var modelData = {};

  main(function () {

    var $tableCarModel = $('#table-carModel');
    util.jqGrid($tableCarModel, {
      url: ajaxURL.ORGANIZATION + '/auto/getAutoModelList',
      datatype: 'json',
      autowidth: true,
      height: $(window).height() - 320,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { name: 'kid', key: true, hidden: true },
        { label: 'ID', name: 'kid', width: 50 },
        {
          label: '车辆品牌', name: 'autoBrandId', width: 150, formatter: function (v, o, r) {
            return dictData.autoBrandId[v] || '';
          }
        },
        { label: '车辆型号', name: 'name', width: 150 },
        {
          label: '状态', name: 'state', width: 80, formatter: function (v, o, r) {
            return dictData.state[v] || '';
          }
        },
        {
          label: '录入时间', name: 'createTime', width: 110, formatter: function (v, o, r) {
            return util.getDate(v);
          }
        }
      ],
      loadComplete: function (res) {
        modelData = {};
        if (res && res.data) {
          $.each(res.data, function (_, item) {
            modelData[item.kid] = item;
          });
        }
      },
      pager: '#pager-carModel'
    });

    // 获取城市列表
    util.ajax(ajaxURL.ORGANIZATION + '/city/getCityList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      util.selectHtml($('[name="cityId"]'), res.data, 'cityId', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.cityId[item.cityId] = item.name;
      });
      searchTable();
    }));

    // 获取车辆品牌列表
    util.ajax(ajaxURL.ORGANIZATION + '/auto/getAutoBrandList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      util.selectHtml($('[name="autoBrandId"]'), res.data, 'kid', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.autoBrandId[item.kid] = item.name;
      });
      searchTable();
    }));

    function searchTable() {
      var data = util.formData('#form-search');
      util.searchGrid($tableCarModel, data);
    };

    $('btn-search').click(searchTable);

    var $layerModel = $('#layer-model-detail');
    var $formModel = $('#form-model-detail');

    // 新增操作
    $('#btn-add').click(function () {
      $layerModel.find(':input').removeAttr('disabled');
      $('#city').addClass('hide');
      util.resetForm($formModel);

      util.layerOpen({
        // title: '新增车辆型号',
        title: false,
        content: $layerModel,
        area: ['500px', ''],
        btn: ['确定新增', '取消'],
        yes: function () {
          modelSave()
        },
        cancel: function (index) {
          layer.confirm('确定退出当前页面？', {
            btn: ['确定', '再想一想'],
            yes: function (idx) {
              layer.close(index);
              layer.close(idx);
            }
          });
          return false;
        }
      });
    });

    // 修改操作
    $('#btn-edit').click(util.oneCheck($tableCarModel, function (id) {
      $layerModel.find(':input').removeAttr('disabled');
      $('#city').removeClass('hide');

      var data = modelData[id];
      util.fillForm($formModel, data);

      util.layerOpen({
        // title: '修改车辆类型',
        title: false,
        content: $layerModel,
        area: ['500px', ''],
        btn: ['确认修改', '取消'],
        yes: function () {
          modelSave()
        },
        cancel: function (index) {
          layer.confirm('确定退出当前页面？', {
            btn: ['确定', '再想一想'],
            yes: function (idx) {
              layer.close(index);
              layer.close(idx);
            }
          });
          return false;
        }
      });
    }));

    var $validate = $formModel.validate({
      rules: {
        autoBrandId: { required: true, maxlength: 20 },
        state: { required: true },
        name: { required: true, maxlength: 20 }
      },
      messages: {
        autoBrandId: { required: '请选择车辆品牌' },
        state: { required: '请选择状态' },
        name: { required: '请输入型号名称' },
      },
      errorPlacement: function (error, element) { //错误信息位置设置方法
        error.appendTo(element.parents('.form-container')); //这里的element是录入数据的对象
      }
    });

    function modelSave() {
      var data = util.formData($formModel);
      if ($validate.form()) {
        var url = ajaxURL.ORGANIZATION + (data.kid ? '/auto/updateAutoModel' : '/auto/addAutoModel');
        util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
          layer.closeAll();
          util.tip.success('操作成功');
          $tableCarModel.trigger('reloadGrid');
        }));
      }
    }

  });
})(window, jQuery);
